﻿using System;

namespace Smugmug
{
    public interface IUploadProgress
    {
        bool Canceled { get; set; }
        void SetProgress(int percentComplete);
        void Completed(Image result);
        void Error(Exception ex);
    }

    public class EventArgs<T> : EventArgs
    {
        public T Value { get; private set; }
        public EventArgs(T value)
        {
            Value = value;
        }
    }

    public class EventUploadProgress : IUploadProgress
    {
        public event EventHandler<EventArgs<Image>> UploadComplete;
        public event EventHandler<EventArgs<Exception>> UploadError;
        public event EventHandler<EventArgs<int>> ProgressChanged;

        protected void OnUploadComplete(Image image)
        {
            var handler = UploadComplete;
            if (handler != null)
            {
                handler(this, new EventArgs<Image>(image));
            }
        }

        protected void OnProgressChanged(int percentComplete)
        {
            var handler = ProgressChanged;
            if (handler != null)
            {
                handler(this, new EventArgs<int>(percentComplete));
            }
        }

        protected void OnUploadError(Exception e)
        {
            var handler = UploadError;
            if (handler != null)
            {
                handler(this, new EventArgs<Exception>(e));
            }
        }

        #region IUploadProgress Members

        public bool Canceled { get; set; }

        public void SetProgress(int percentComplete)
        {
            OnProgressChanged(percentComplete);
        }

        public void Completed(Image result)
        {
            OnUploadComplete(result);
        }

        public void Error(Exception ex)
        {
            OnUploadError(ex);
        }

        #endregion
    }

}